<template>
<div>
  <el-button style="position: absolute;left: 330px"
             type="primary" plain @click="dialogFormVisible1=true">添加</el-button>
  <el-dialog title="角色修改" :visible.sync="dialogFormVisible1">
    <el-form :model="roleadd">
      <el-form-item label="名称" >
        <el-input v-model="roleadd.rname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" >
        <el-input v-model="roleadd.annotation" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <template>
          <el-select v-model="roleadd.state" placeholder="请选择" >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible1 = false">取 消</el-button>
      <el-button type="primary" @click="add()">确 定</el-button>
    </div>
  </el-dialog>
  <br>
  <br>
  <br>
  <el-table
    :data="tableData"
    stripe
    style="width: 100%">
    <el-table-column
      prop="rid"
      label="编号"
      width="180">
    </el-table-column>
    <el-table-column
      prop="rname"
      label=角色名字
      width="180">
    </el-table-column>
    <el-table-column
      prop="annotation"
      label="备注"
      width="180">
    </el-table-column>
    <el-table-column label="状态">
      <template slot-scope="scope">
        <span v-if="scope.row.state == 0">未启用</span>
        <span v-if="scope.row.state == 1">启用中</span>
      </template>
    </el-table-column>


    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-edit"
          @click="dialogFormVisible=true;roleupd=scope.row"></el-button>
        <el-button
          size="mini"
          type="danger" icon="el-icon-delete"
          @click="del(scope.row)"></el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    small
    layout="total,prev, pager, next"
    :page-size="pageSize"
    :total="total" @current-change="page">
  </el-pagination>
  <el-dialog title="角色修改" :visible.sync="dialogFormVisible">
    <el-form :model="roleupd">
      <el-form-item label="名称" >
        <el-input v-model="roleupd.rname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" >
        <el-input v-model="roleupd.annotation" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <template>
          <el-select v-model="roleupd.state" placeholder="请选择" >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="upd">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "Role",
  data(){
    return{
      total: 0,
      pageSize: 6,
      tableData: [],
      dialogFormVisible1:false,
      dialogFormVisible:false,
      roleadd:{rname:"",annotation:"",state:""},
      roleupd:{},
      options: [{
        value: '0',
        label: '未启用'
      },{
        value: '1',
        label: '启用中'
      }]
    }
  },
  created() {
      this.find()
  },
  methods:{
    find(){
      this.$http.post("backservice/role/findpage?pageNum=0&pageSize=" + this.pageSize).then(response => {
        console.log(response.userList)
        this.tableData = response.userList
        this.total = response.TotalNum
      })
    },
    page(currentPage) {
      this.$http.post('backservice/role/findpage?&pageNum=' + (currentPage - 1) + '&pageSize=' + this.pageSize)
        .then(response => {
          this.tableData = response.userList
        })
    },
    add(){
      this.dialogFormVisible1=false
      this.$http.post('backservice/role/add',this.roleadd).then(data=>{
        if (data>0){
          this.find()
          alert("添加成功")
        }else {
          alert("添加失败")
        }
      })
    },
    upd(){
      this.dialogFormVisible=false
      this.$http.post('backservice/role/update',this.roleupd).then(data=>{
        if (data>0){
          this.find()
          alert("修改成功")
        }else {
          alert("修改失败")
        }
      })
    },
    del(row){
      this.$http.post('backservice/role/delete',{rid:row.rid}).then(data=>{
        if (data>0){
          this.find()
          alert("删除成功")
        }else {
          alert("删除失败")
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
